import java.awt.*;

import javax.swing.*;

public class TerrainLayer extends JPanel {
	Resource[] resources;
	Cursor cursor;
	Decal[] decals;
	String[] map;
	String[] imageFile = { "black.gif", "grass.png", "water.png", "sand.png",
			"rock.png" };
	Image[] image = new Image[imageFile.length];

	String[] resourceImageFile = {"gold.png"};
	Image[] resourceImage = new Image[resourceImageFile.length];
	String[] decalImageFile = { "decal1.png" };
	Image[] decalImage = new Image[decalImageFile.length];

	public TerrainLayer(String[] map, Decal[] decals, Resource[] resources, Cursor cursor) {
		this.resources=resources;
		this.cursor = cursor;
		this.decals = decals;
		this.map = map;
		this.setFocusable(false);

		// Initialize images from their file names
		MediaTracker media = new MediaTracker(this);
		for (int i = 0; i < imageFile.length; i++) {
			image[i] = Toolkit.getDefaultToolkit().getImage(
					"graphics\\" + imageFile[i]);
			media.addImage(image[i], 0);
			try {
				media.waitForID(0);
			} catch (Exception e) {
			}
		}
		for (int i = 0; i < resourceImageFile.length; i++) {
			resourceImage[i] = Toolkit.getDefaultToolkit().getImage(
					"graphics\\" + resourceImageFile[i]);
			media.addImage(resourceImage[i], 0);
			try {
				media.waitForID(0);
			} catch (Exception e) {
			}
			
			
						
			
		}
		
		
		for (int i = 0; i < decalImageFile.length; i++) {
			decalImage[i] = Toolkit.getDefaultToolkit().getImage(
					"graphics\\" + decalImageFile[i]);
			media.addImage(decalImage[i], 0);
			try {
				media.waitForID(0);
			} catch (Exception e) {
			}
		}
		
		
	}

	public void paint(Graphics g) {
		this.setLocation(0, 0);
		int w = 850, h = 550;

		this.setSize(w, h);

		// Create display of map area of size 17x11 with cursor as the middle
		// point
		for (int i = 0; i < 17; i++) {
			for (int j = 0; j < 11; j++) {
				int posx = 8, posy = 5;
				try {
					posx = i - 8 + cursor.getx();
					posy = j - 5 + cursor.gety();
				} catch (Exception e) {
				}
				;
				int img_n = 0;
				if ((posx < 0) || (posx > cursor.getmx()) || (posy < 0)
						|| (posy > cursor.getmx())) {
					// Outside of map bounds, display black tile
				} else {
					switch (map[posy].charAt(posx)) {
					case '.':
						img_n = 1;
						break;
					case '*':
						img_n = 2;
						break;
					case '$':
						img_n = 3;
						break;
					case '&':
						img_n = 4;
						break;
					}
				}

				// Draw tile
				g.drawImage(image[img_n], i * 50, j * 50, this);

			}
		}

		int posx, posy;
		posx = -8 + cursor.getx();
		posy = -5 + cursor.gety();
		
		
		// Populate the map with resource images
		for (int i = 0; i < resources.length; i++) {
			if (resources[i] == null) {

			} else {
						
				int x = (resources[i].getX() - posx);
				int y = (resources[i].getY() - posy);
				if ((x < 0) || (x > 16) || (y < 0) || (y > 16)) {
					// Outside of areaView bounds
				} else {
					// Draw building image
					g.drawImage(resourceImage[resources[i].getType() - 70], x * 50,
							y * 50, this);
				}
			}
		}
		

		// Populate the map with item images
		for (int i = 0; i < decals.length; i++) {
			if (decals[i] == null) {

			} else {
				int x = (decals[i].getX() - posx);
				int y = (decals[i].getY() - posy);
				if ((x < 0) || (x > 16) || (y < 0) || (y > 16)) {
					// Outside of areaView bounds
				} else {
					// Draw building image

					g.drawImage(decalImage[decals[i].getType() - 50], x * 50,
							y * 50, this);
				}
			}
		}


		g.drawRect(0, 0, w - 1, h - 1);
	}
}
